/*
 * bspTimer.h
 *
 *  Created on: 2017年9月20日
 *      Author: Administrator
 */

#ifndef BSPUSER_BSPTIMER_H_
#define BSPUSER_BSPTIMER_H_
/*   MCU_LIB_include    */
#include "bspConfig.h"

typedef void (*timerCallback)(uint32_t value);


typedef struct 
{
	timerCallback tvCb0;
	timerCallback tvCb1;
	timerCallback tvCb2;
	timerCallback tvCb3;
	timerCallback tvCb4;
	timerCallback tvCb5;
	timerCallback tvCb6;	
}tsCbTimer;
extern tsCbTimer tgTimer;





#if BSP_MCU_TYPE == BSP_MCU_GD32E23x	
typedef struct 
{
	rcu_periph_enum timerRcu;	
	uint32_t timerPeriph;
	uint8_t nvicIrq;
    uint8_t nvicIrqPriority;
	timerCallback *tvCb;
}tsCfgTimer;

#define DEV_TIMER0 { \
		RCU_TIMER0,TIMER0,TIMER0_Channel_IRQn,0,&tgTimer.tvCb0}
		
#define DEV_TIMER1 { \
		RCU_TIMER2,TIMER2,TIMER2_IRQn,0,&tgTimer.tvCb1}

#define DEV_TIMER2 { \
		RCU_TIMER5,TIMER5,TIMER5_IRQn,0,&tgTimer.tvCb2}
		
#define DEV_TIMER3 { \
		RCU_TIMER13,TIMER13,TIMER13_IRQn,0,&tgTimer.tvCb3}
		
#define DEV_TIMER4 { \
		RCU_TIMER14,TIMER14,TIMER14_IRQn,0,&tgTimer.tvCb4}
		
#define DEV_TIMER5 { \
		RCU_TIMER15,TIMER15,TIMER15_IRQn,0,&tgTimer.tvCb5}

#define DEV_TIMER6 { \
		RCU_TIMER16,TIMER16,TIMER16_IRQn,0,&tgTimer.tvCb6}	
		
#elif BSP_MCU_TYPE == BSP_MCU_GD32F3x0		
typedef struct 
{
	rcu_periph_enum timerRcu;	
	uint32_t timerPeriph;
	uint8_t nvicIrq;
    uint8_t nvicIrqPriority;
	uint8_t nvicIrqSubPriority;
	
	timerCallback *tvCb;
}tsCfgTimer;

#define DEV_TIMER0 { \
		RCU_TIMER0,TIMER0,TIMER0_Channel_IRQn,0,0,&tgTimer.tvCb0}

#define DEV_TIMER1 { \
		RCU_TIMER2,TIMER2,TIMER2_IRQn,0,0,&tgTimer.tvCb1}
		
#define DEV_TIMER2 { \
		RCU_TIMER1,TIMER1,TIMER1_IRQn,0,0,&tgTimer.tvCb2}

#define DEV_TIMER3 { \
		RCU_TIMER13,TIMER13,TIMER13_IRQn,0,0,&tgTimer.tvCb3}
		
#define DEV_TIMER4 { \
		RCU_TIMER14,TIMER14,TIMER14_IRQn,0,0,&tgTimer.tvCb4}
		
#define DEV_TIMER5 { \
		RCU_TIMER15,TIMER15,TIMER15_IRQn,0,0,&tgTimer.tvCb5}

#define DEV_TIMER6 { \
		RCU_TIMER16,TIMER16,TIMER16_IRQn,0,0,&tgTimer.tvCb6}	
#endif


extern void initTimer(tsCfgTimer *cfg, uint32_t freq, timerCallback ptTimerCb);

		/* 捕获初始化
 * freq:频率
 * channel:通道 0~3
 * polarity捕获边沿.
 *		TIMER_IC_POLARITY_RISING
 *		TIMER_IC_POLARITY_FALLING
 *		TIMER_IC_POLARITY_BOTH_EDGE
 * ptTimerCallback:定时器回调
 * return:NULL
 */
extern void initCapture(tsCfgTimer *cfg, uint32_t channel, uint32_t freq, uint32_t polarity, timerCallback ptTimerCb);

extern void timerCaptureEn(tsCfgTimer *cfg, uint32_t channel);		
extern void timerCaptureDis(tsCfgTimer *cfg, uint32_t channel);

extern void timerStart(tsCfgTimer *cfg);
extern void timerStop(tsCfgTimer *cfg);
		
#endif /* BSPUSER_BSPTIMER_H_ */
